import request from '@/utils/request'
import type {
  CommonResult,
  PageResult,
  CodeVO,
  CodeAddRequest,
  CodeUpdateRequest,
  CodeQueryRequest,
  BaseRequest,
} from '@/types/api'

/**
 * 新增代码
 */
export function addCode(data: CodeAddRequest): Promise<CommonResult<number>> {
  return request({
    url: '/code/add',
    method: 'post',
    data,
  })
}

/**
 * 删除代码
 */
export function deleteCode(data: BaseRequest): Promise<CommonResult<boolean>> {
  return request({
    url: '/code/delete',
    method: 'post',
    data,
  })
}

/**
 * 更新代码
 */
export function updateCode(data: CodeUpdateRequest): Promise<CommonResult<boolean>> {
  return request({
    url: '/code/update',
    method: 'post',
    data,
  })
}

/**
 * 分页查询代码
 */
export function pageCode(params: CodeQueryRequest): Promise<CommonResult<PageResult<CodeVO>>> {
  return request({
    url: '/code/page',
    method: 'get',
    params,
  })
}

/**
 * 获取代码详情
 */
export function getCodeById(id: string): Promise<CommonResult<CodeVO>> {
  return request({
    url: '/code/get',
    method: 'get',
    params: { id },
  })
}

/**
 * 增加代码片段浏览次数
 */
export function incrementViewCount(id: string): Promise<CommonResult<boolean>> {
  return request({
    url: '/code/view',
    method: 'post',
    params: { id },
    skipErrorMessage: true, // 不显示错误提示
  })
}
